import $mapevent from "./mapevent";
import $fetch from "./request";
import { mainStore } from "../store/index";
const store = mainStore();
import { ElMessage, ElMessageBox } from "element-plus";
/**
 * 标点
 */
async function drop() {
    return $fetch("/mapLabel/list", {}, "post", localStorage.getItem("token")).then(
        (red) => {
            $mapevent.f_ClearEntities(1011);
            if (red.code == 200) {
                red.data.forEach((element) => {
                    $mapevent.f_AddIconLabel(element, element.id, 1011);
                });
            }
        }
    );
}
/**
 * 添加标点
 */
async function dropAdd(embryo) {
    let { x, y, z } = store.punctuation.position3d;
    let data = {
        sightdistance: embryo.SightDistance,
        iconImg: embryo.iconImg,
        surround: embryo.surround,
        borderSize: embryo.borderSize,
        position: JSON.stringify({ x, y, z }),
        borderColor: embryo.borderColor,
        fontColor: embryo.fontColor,
        fontSize: embryo.fontSize,
        name: embryo.name,
        type:embryo.type,
    };
    return $fetch("/mapLabel/add", data, "post", localStorage.getItem("token")).then(
        (red) => {
            if (red.code == 200) {
                drop();
                ElMessage({
                    type: "success",
                    message: "添加成功！",
                });
            }
        }
    );
}
/**
 * 编辑标点
 */
async function dropEdit(embryo) {
    let data = {
        sightdistance: embryo.SightDistance,
        iconImg: embryo.iconImg,
        surround: embryo.surround,
        borderSize: embryo.borderSize,
        borderColor: embryo.borderColor,
        fontColor: embryo.fontColor,
        fontSize: embryo.fontSize,
        name: embryo.name,
        id: store.tagEntity.embryo.id,
    };
    return $fetch("/mapLabel/edit", data, "post", localStorage.getItem("token")).then(
        (red) => {
            if (red.code == 200) {
                ElMessage({
                    type: "success",
                    message: "修改成功！",
                });
                drop();
            }
        }
    );
}
/**
 * 删除标点
 */
async function dropDelete(id) {
    return $fetch(
        `/mapLabel/delete/${id}`,
        {},
        "get",
        localStorage.getItem("token")
    ).then((red) => {
        if (red.code == 200) {
            $mapevent.f_ClearEntities(1011, id);
        }
    });
}
/**
 * 标绘
 */
async function plotting() {
    return $fetch("/mapvector/list", {}, "post", localStorage.getItem("token")).then(
        (red) => {
            $mapevent.f_ClearEntities(1012);
            $mapevent.f_ClearEntities(1013);
            if (red.code == 200) {
                red.data.forEach((element) => {
                    if (element.type == "0") {
                        $mapevent.drawPolylines(
                            {
                                ...element,
                                lineWidth: element.frameWidth,
                                lineColor: element.frameColor,
                            },
                            element.id,
                            1012
                        );
                    } else $mapevent.drawPolygon(element, element.id, 1013);
                });
            }
        }
    );
}
/**
 * 添加标绘
 */
async function plottingAdd(embryo) {
    let data = {
        name: embryo.name,
        planeColor: embryo.planeColor,
        sightdistance: embryo.sightdistance || 2000000,
        type: embryo.type,
        frameColor: embryo.type == 0 ? embryo.lineColor : embryo.frameColor,
        frameWidth: embryo.type == 0 ? embryo.lineWidth : embryo.frameWidth,
        position: JSON.stringify(store.punctuation.position),
        surround: null,
    };
    return $fetch("/mapvector/add", data, "post", localStorage.getItem("token")).then(
        (red) => {
            if (red.code == 200) {
                ElMessage({
                    type: "success",
                    message: "添加成功！",
                });
                plotting();
            }
        }
    );
}
/**
 * 编辑标绘
 */
async function plottingEdit(embryo) {
    let data = {
        name: embryo.name,
        planeColor: embryo.planeColor,
        sightdistance: embryo.sightdistance,
        id: store.tagEntity.embryo.id,
        frameColor: embryo.type == 0 ? embryo.lineColor : embryo.frameColor,
        frameWidth: embryo.type == 0 ? embryo.lineWidth : embryo.frameWidth,
    };
    return $fetch("/mapvector/edit", data, "post", localStorage.getItem("token")).then(
        (red) => {
            if (red.code == 200) {
                ElMessage({
                    type: "success",
                    message: "修改成功！",
                });
                plotting();
            }
        }
    );
}
/**
 * 删除标绘
 */
async function plottingDelete(type, id) {
    return $fetch(
        `/mapvector/delete/${id}`,
        {},
        "get",
        localStorage.getItem("token")
    ).then((red) => {
        if (red.code == 200) {
            if (!type) $mapevent.f_ClearEntities(1012, id);
            else $mapevent.f_ClearEntities(1013, id);
        }
    });
}
/**
 * 立体字
 */
async function stereoscopic() {
    return $fetch("/p3dtext/list", {}, "post", localStorage.getItem("token")).then(
        (red) => {
            $mapevent.f_ClearEntities(1014);
            if (red.code == 200) {
                red.data.forEach((element) => {
                    let embryo = {
                        stereoscopicColor: element.color,
                        position: element.coordinate,
                        name: element.name,
                        id: element.id
                    };
                    $mapevent.f_Add3DStereoscopic(embryo, element.id, 1014);
                });
            }
        }
    );
}
/**
 * 添加立体字
 */
async function stereoscopicAdd(embryo) {
    let data = {
        name: embryo.name,
        color: embryo.stereoscopicColor,
        coordinate: JSON.stringify(store.punctuation.position),
    };
    return $fetch("/p3dtext/add", data, "post", localStorage.getItem("token")).then(
        (red) => {
            if (red.code == 200) {
                ElMessage({
                    type: "success",
                    message: "添加成功！",
                });
                stereoscopic();
            }
        }
    );
}
/**
 * 编辑立体字
 */
async function stereoscopicEdit(embryo) {
    console.log(store);
    let data = {
        name: embryo.name,
        color: embryo.stereoscopicColor,
        id: store.tagEntity.embryo.id,
    };
    return $fetch("/p3dtext/edit", data, "post", localStorage.getItem("token")).then(
        (red) => {
            if (red.code == 200) {
                ElMessage({
                    type: "success",
                    message: "修改成功！",
                });
                stereoscopic();
            }
        }
    );
}
/**
 * 删除立体字
 */
async function stereoscopicDelete(id) {
    return $fetch(
        `/p3dtext/delete/${id}`,
        {},
        "get",
        localStorage.getItem("token")
    ).then((red) => {
        if (red.code == 200) {
            $mapevent.f_ClearEntities(1014, id);
        }
    });
}
export default {
    drop,
    dropAdd,
    dropEdit,
    dropDelete,
    plotting,
    plottingAdd,
    plottingEdit,
    plottingDelete,
    stereoscopic,
    stereoscopicAdd,
    stereoscopicEdit,
    stereoscopicDelete,
};
